//
//  AboutViewController.swift
//  SitPosition
//
//  Created by apple on 2019/1/19.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


class AboutViewController : GKNavigationBarViewController {
    
    // MARK: - Properties
    
    // Logo
    lazy var logoIV: UIImageView = {
        let image = UIImage(named: "logo_s")
        let iv = UIImageView(image: image)
        iv.contentMode = .scaleAspectFit
        return iv
    }()
    
    // 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.textAlignment = .center
        label.lineBreakMode = .byTruncatingTail
        label.font = Theme.Font.mediumFont(ofSize: 16)
        label.textColor = Theme.Color.primaryDarkTextColor
        label.backgroundColor = UIColor.clear
        label.text = Strings.aboutTitle
        return label
    }()
    
    // 子标题
    lazy var subtitleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.textAlignment = .center
        label.lineBreakMode = .byTruncatingTail
        label.font = Theme.Font.mediumFont(ofSize: 14)
        label.textColor = Theme.Color.lighgrayTextColor
        label.backgroundColor = UIColor.clear
        label.text = Strings.aboutSubtitle
        return label
    }()
    
    // 正文背景
    lazy var contentBKView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 5
        return view
    }()
    
    // 正文
    lazy var contentTV: UITextView = {
        let tv = UITextView()
        tv.isScrollEnabled = true
        tv.textAlignment = .left
        tv.font = Theme.Font.mediumFont(ofSize: 14)
        tv.textColor = Theme.Color.primaryDarkTextColor
        tv.backgroundColor = UIColor.clear
        tv.text = Strings.aboutContent
        return tv
    }()
    
    // 底部标题
    lazy var footerTitleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.textAlignment = .center
        label.lineBreakMode = .byTruncatingTail
        label.font = Theme.Font.mediumFont(ofSize: 14)
        label.textColor = Theme.Color.lighgrayTextColor
        label.backgroundColor = UIColor.clear
        label.text = Strings.aboutFooterTitle
        return label
    }()
    
    // 底部子标题
    lazy var footerSubtitleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.textAlignment = .center
        label.lineBreakMode = .byTruncatingTail
        label.font = Theme.Font.mediumFont(ofSize: 14)
        label.textColor = Theme.Color.lighgrayTextColor
        label.backgroundColor = UIColor.clear
        label.text = Strings.aboutFooterSubtitle
        return label
    }()
    
    
    // MARK: - Object lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 导航栏
        gk_navLineHidden = true
        gk_navBackgroundColor = UIColor.white
        gk_navTitle = Strings.aboutUs
        gk_navLeftBarButtonItem = UIBarButtonItem(imageName: "ic_nav_back_black", target: self, action: #selector(_backButtonTapped))
        
        // 设置子视图
        contentBKView.addSubview(contentTV)
        view.addSubviews([logoIV,
                          titleLabel,
                          subtitleLabel,
                          contentBKView,
                          footerTitleLabel,
                          footerSubtitleLabel])
        _setupConstraints()
        view.backgroundColor = Theme.Color.primaryBackgroundColor
    }

}


// MARK: - Private Methods

extension AboutViewController {
    
    // 设置视图约束
    func _setupConstraints() {
        // logoIV
        constrain(logoIV, gk_navigationBar, view) { v, sibling, parent in
            v.top == sibling.bottom + 26
            v.centerX == parent.centerX
            v.width == 83
            v.height == 83
        }
        
        // titleLabel
        constrain(titleLabel, logoIV, view) { v, sibling, parent in
            v.top == sibling.bottom + 12
            v.left == parent.left + 15
            v.right == parent.right - 15
        }
        
        // subtitleLabel
        constrain(subtitleLabel, titleLabel, view) { v, sibling, parent in
            v.top == sibling.bottom + 12
            v.left == parent.left + 15
            v.right == parent.right - 15
        }
        
        // footerSubtitleLabel
        constrain(footerSubtitleLabel, view) { v, parent in
            v.bottom == parent.bottom - 28
            v.left == parent.left + 15
            v.right == parent.right - 15
        }
        
        // footerTitleLabel
        constrain(footerTitleLabel, footerSubtitleLabel, view) { v, sibling, parent in
            v.bottom == sibling.top - 7
            v.left == parent.left + 15
            v.right == parent.right - 15
        }
        
        // contentBKView
        constrain(contentBKView, subtitleLabel, footerTitleLabel, view) { v, sibling, siblingBottom, parent in
            v.top == sibling.bottom + 23
            v.left == parent.left + 15
            v.right == parent.right - 15
            v.height == 275 ~ UILayoutPriority.defaultLow
            v.bottom <= siblingBottom.top ~ UILayoutPriority.required
        }
        
        constrain(contentTV, contentBKView) { v, parent in
            v.edges == inset(parent.edges, 22, 22, 22, 22)
        }
    }
    
    // 返回按钮点击事件
    @objc func _backButtonTapped() {
        if presentingViewController != nil {
            dismiss(animated: true, completion: nil)
        } else {
            navigationController?.popViewController(animated: true)
        }
    }
}
